package sui.template
{

    public class MathExp implements IExpression
    {

        public static var ADD:int = 1;

        public static var SUBSTRACT:int = 2;

        public static var MULTIPLY:int = 3;

        public static var MOD:int = 4;

        public static var DIVIDE:int = 5;

        private var expArr:Array;

        private var operator:int;

        public function MathExp(expArr:Array, operator:int)
        {
            this.expArr = expArr;
            this.operator = operator;
        }

        public function exec(context:Object):*
        {
            var res:Number = IExpression(expArr[0]).exec(context);
            for (var i:int = 1; i < expArr.length; i++)
            {
                var value:Number = IExpression(expArr[i]).exec(context);
                switch (operator)
                {
                    case ADD:
                    {
                        res += value;
                        break;
                    }
                    case SUBSTRACT:
                    {
                        res -= value;
                        break;
                    }
                    case MULTIPLY:
                    {
                        res *= value;
                        break;
                    }
                    case DIVIDE:
                    {
                        res /= value;
                        break;
                    }
                    case MOD:
                    {
                        res %= value;
                        break;
                    }
                }
            }
            return res;
        }
    }
}
